# Copyright 2021-2023 @ Shenzhen Bay Laboratory &
#                       Peking University &
#                       Huawei Technologies Co., Ltd
#
# This code is a part of Cybertron package.
#
# The Cybertron is open-source software based on the AI-framework:
# PyTorch (https://pytorch.org/)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""
Residual decoder networks for readout function
"""

from typing import Union, List

import torch
from torch import nn
from torch import Tensor

from ..utils import get_arguments
from .decoder import Decoder, _decoder_register
from ..layer import PreActResidual
from ..layer import SeqPreActResidual
from ..layer import PreActDense


@_decoder_register('residual')
class ResidualOutputBlock(Decoder):
    r"""A residual decoder network.

    Args:
        dim_in (int): Input dimension.
        dim_out (int): Output dimension. Default: 1
        activation (Union[nn.Module, str]): Activation function. Default: None
        n_layers (int): Number of hidden layers. Default: 1
        n_res_layers (int): Number of residual blocks. Default: 1
    """
    def __init__(self,
                 dim_in: int,
                 dim_out: int = 1,
                 activation: Union[nn.Module, str] = None,
                 n_layers: int = 1,
                 **kwargs,
                 ):

        super().__init__(
            dim_in=dim_in,
            dim_out=dim_out,
            activation=activation,
            n_layers=n_layers,
        )
        self._kwargs = get_arguments(locals(), kwargs)

        if self.n_layers == 1:
            output_residual = PreActResidual(self.dim_in, activation=self.activation)
        else:
            output_residual = SeqPreActResidual(
                self.dim_in, activation=self.activation, n_res=self.n_layers
                )

        self.output = nn.Sequential(
            output_residual,
            PreActDense(self.dim_in, self.dim_out, activation=self.activation),
        )

    def __str__(self):
        return 'ResidualOutputBlock<>'
